package edu.uoc.tdp.pac4.client.logandreg;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.beans.Registration;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;


/**
 * Class to help the filling of comboboxes
 *
 */
public class NewRegistrationHelper {

	public DefaultComboBoxModel getComboCoursesModel(ClientConnectionHelper connectionHelper){

		HashMap<String, Course> courses = getMap(connectionHelper);
		try {
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		model.addElement(null);
		//
		Set<String> keys = courses.keySet();
        Iterator<String> it = keys.iterator();
        while(it.hasNext()) {
            String key = it.next();
            model.addElement(key);
        }	
		return model;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public DefaultComboBoxModel getComboGroupsModel(ClientConnectionHelper connectionHelper, Course course){
		
		HashMap<String, Group> groups = getMapGroups(connectionHelper, course);
		try {
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		model.addElement(null);
		//
		Set<String> keys = groups.keySet();
        Iterator<String> it = keys.iterator();
        while(it.hasNext()) {
            String key = it.next();
            model.addElement(key);
        }	
		return model;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	HashMap<String, Course> getMap(ClientConnectionHelper connectionHelper) {
		
		List<Course> courses;
        try {
			courses = connectionHelper.getMaintenanceManager().listCourse();
			HashMap<String, Course> map = new HashMap<String, Course>();
			for (Iterator<Course> it = courses.iterator() ; it.hasNext();) {
				Course c = it.next();
				String key = c.getCourseName();
	            map.put(key, c);	
			}
			return map;
        } catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return null;
	}
	
HashMap<String, Group> getMapGroups(ClientConnectionHelper connectionHelper, Course course) {
		
		List<Group> groups;
        try {

			groups = connectionHelper.getLogAndRegManager().getNoStartedGroupsByCourse(course);
			HashMap<String, Group> map = new HashMap<String, Group>();
			for (Iterator<Group> it = groups.iterator() ; it.hasNext();) {
				Group g = it.next();
				String key = g.getName();
	            map.put(key, g);	
			}
			return map;
        } catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return null;
	}


	public void registerNewReg(ClientConnectionHelper connectionHelper, Group group){
		Registration registration = null;
		try{
			registration = new Registration();
			registration.setRequestId(1);
			registration.setCourseId(group.getCourse().getCourseCode());
			registration.setGroupId(group.getGroupId());
			registration.setStatus(0);
			registration.setUserId(ClientConnectionHelper.getLoggedUser().getNIF()); 
			if(connectionHelper.getLogAndRegManager().checkNewRegistration(registration)){
				JOptionPane.showMessageDialog(null, 
						LanguageUtils.getMessage("client.regandlog.title.regok"),
						LanguageUtils.getMessage("client.regandlog.info.regok"),
						JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				JOptionPane.showMessageDialog(null, 
						LanguageUtils.getMessage("client.regandlog.warning.regexists"),
						LanguageUtils.getMessage("client.regandlog.warning.title"),
						JOptionPane.WARNING_MESSAGE);
			}
			
				
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
	}
}
